from pylab import *
import matplotlib.pyplot as plt

plt.interactive(True)

# complex code which produces multiple figures goes here...

# # ... and might do something like 
# fig1 = plt.figure()
# plt.plot(range(10))
# plt.draw()

# # ... or this
# fig2 = plt.figure()
# plt.polar(range(10))
# plt.draw()

# # once everything is done, we can put in a blocking call
# # which will terminate when the last window is closed by the user
# # plt.show(block=dsfsdTrue)
# plt.show(block=False)

import time
# time.sleep(5)
from random import random
figure()
plt.show(block=False)
        
# while True:
#     s = raw_input()
#     print "you input : ", s
#     if s == "new":
#         x = array(range(100))
#         m = array(random()*3)
#         plt.plot(x,x**m)
#         plt.show(block=False)

# for i in range(10):
#     x = array(range(100))
#     m = array(random()*3)
#     plt.plot(x,x**m)
#     plt.show(block=False)
#     print "OK"
#     time.sleep(2)
    
# plt.show()

x = linspace(0,10,100)


def plot_some(cmd):
    hold(False)
    if cmd == "sin":
        plot(x,sin(x))
    elif cmd == "cos":
        plot(x,cos(x))


import socket
sockin = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockin.bind(("127.0.0.1", 8080))

print "waiting for msg"
while True:
    msg, (addr,port) = sockin.recvfrom(100)
    print "receive : ", msg
    # if msg == "test":
    #     m = array(random()*3)
    #     plt.plot(x,x**m)
    #     plot_some()
    #     plt.draw()
    #     plt.show(block=False)

    plot_some(msg)
    plt.draw()
    plt.show(block=False)

        
